4. Heatmap¶
4.2. Plotlyによる作図方法¶
Plotlyでは,plotly.express.density_heatmap()を用いて作図できます.
import plotly.express as px
fig = px.density_heatmap(
df, x='col_x', y='col_y', z='col_z')
上記の例では,dfのcol_xを横軸,col_yを縦軸とし,col_zの量に応じて色を塗り分けたHeatmapのオブジェクトfigを作成します.
4.3. MADB Labを用いた作図例¶
4.3.1. 下準備¶
import pandas as pd
import plotly.express as px
import warnings
warnings.filterwarnings('ignore')
# 前処理の結果,以下に分析対象ファイルが格納されていることを想定
PATH_DATA = '../../data/preprocess/out/magazines.csv'
# Jupyter Book用のPlotlyのrenderer
RENDERER = 'plotly_mimetype+notebook'
def show_fig(fig):
"""Jupyter Bookでも表示可能なようRendererを指定"""
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
fig.show(renderer=RENDERER)
def add_years_to_df(df, unit_years=10):
"""unit_years単位で区切ったyears列を追加"""
df_new = df.copy()
df_new['years'] = \
df['datePublished'].dt.year // unit_years * unit_years
df_new['years'] = df_new['years'].astype(str)
return df_new
df = pd.read_csv(PATH_DATA)
4.3.2. 作品別・年代別の合計連載週数(上位20作品)¶
# datePublishedを10年単位で区切るyears列を追加
df['datePublished'] = pd.to_datetime(df['datePublished'])
# 10年単位で区切ったyearsを追加
df = add_years_to_df(df)
# プロット用に集計
df_plot = df.groupby('cname')['years'].value_counts().\
reset_index(name='weeks')
# 連載週刊上位10作品を抽出
cnames = list(df.value_counts('cname').head(20).index)
df_plot = df_plot[df_plot['cname'].isin(cnames)].\
reset_index(drop=True)
# 合計連載週数で降順ソート
df_plot['order'] = df_plot['cname'].apply(
lambda x: cnames.index(x))
df_plot = df_plot.sort_values(
['order', 'years'], ignore_index=True)
fig = px.density_heatmap(
df_plot, x='cname', y='years', z='weeks')
show_fig(fig)
4.3.3. 作者別・年代別の合計連載週数(上位20名)¶
# datePublishedを10年単位で区切るyears列を追加
df['datePublished'] = pd.to_datetime(df['datePublished'])
# 10年単位で区切ったyearsを追加
df = add_years_to_df(df)
# プロット用に集計
df_plot = df.groupby('creator')['years'].value_counts().\
reset_index(name='weeks')
# 連載週刊上位10作品を抽出
creators = list(df.value_counts('creator').head(20).index)
df_plot = df_plot[df_plot['creator'].isin(creators)].\
reset_index(drop=True)
# 合計連載週数で降順ソート
df_plot['order'] = df_plot['creator'].apply(
lambda x: creators.index(x))
df_plot = df_plot.sort_values(
['order', 'years'], ignore_index=True)
fig = px.density_heatmap(
df_plot, x='creator', y='years', z='weeks')
show_fig(fig)
